package cn.stylefeng.guns.sys.modular.dbList.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.dbList.entity.Dblist;
import cn.stylefeng.guns.sys.modular.dbList.mapper.DblistMapper;
import cn.stylefeng.guns.sys.modular.dbList.model.params.DblistParam;
import cn.stylefeng.guns.sys.modular.dbList.model.result.DblistResult;
import cn.stylefeng.guns.sys.modular.dbList.service.DblistService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 数据库列表  服务实现类
 * </p>
 *
 * @author author
 * @since 2020-08-20
 */
@Service
public class DblistServiceImpl extends ServiceImpl<DblistMapper, Dblist> implements DblistService {

    @Override
    public void add(DblistParam param) {
        Dblist entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DblistParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(DblistParam param) {
        Dblist oldEntity = getOldEntity(param);
        Dblist newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DblistResult findBySpec(DblistParam param) {
        return null;
    }

    @Override
    public List<DblistResult> findListBySpec(DblistParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DblistParam param) {
        Page pageContext = getPageContext();
        if (StringUtils.isEmpty(param.getSpState())) {
            param.setSpState("1");
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public Integer alterDbListInfoByServiceId(DblistParam dblistParam) {
        return this.baseMapper.alterDbListByServiceId(dblistParam);
    }

    @Override
    public DblistResult searchDbListInfoByServiceId(DblistParam dblistParam) {
        return this.baseMapper.searchDbListByServiceId(dblistParam);
    }

    private Serializable getKey(DblistParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Dblist getOldEntity(DblistParam param) {
        return this.getById(getKey(param));
    }

    private Dblist getEntity(DblistParam param) {
        Dblist entity = new Dblist();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
